package org.come.mouslisten;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

import org.come.Frame.OptionsJframe;
import org.come.Frame.RoleMsgJframe;
import org.come.Frame.WorldTestsmallmapJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.FrameMessageChangeJpanel;
import org.come.Jpanel.GameJpanel;
import org.come.Jpanel.TestsmallmapJpanel;
import org.come.bean.Coordinates;
import org.come.bean.Path;
import org.come.bean.PathPoint;
import org.come.bean.RoleShow;
import org.come.control.JirenjiaobenchuliControl;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.thread.TimeControlRunnable;
import org.come.until.*;

import com.tool.image.ImageMixDeal;
import com.tool.image.ManimgAttribute;
import com.tool.imagemonitor.FightingMonitor;
import com.tool.imagemonitor.PlayerMonitor;
import com.tool.imagemonitor.ScriptOpen;
import com.tool.tcpimg.InputBean;
import come.tool.BangBattle.BangFight;
import come.tool.Fighting.FightingMixDeal;
import come.tool.JDialog.TiShiUtil;
import come.tool.Scene.Scene;
import come.tool.handle.HandleState;
import come.tool.map.XLPath;

public class Mouselistener implements MouseMotionListener,MouseListener {
	int xd = 0;
	int yd = 0;
	public boolean yidong = false;

	private InputBean inputBean;

	public Mouselistener() {
		super();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
        if (inputBean!=null) {
        	DJInputBean(inputBean);
        	inputBean=null;
		}else if (FightingMixDeal.State == HandleState.USUAL&&e.getButton() == MouseEvent.BUTTON3) {
			if (yidong) {
				int x = e.getX();
				int y = e.getY();
				int xdd = (Util.mapmodel.getShot_x() + x) / 20;
				int ydd = (Util.mapmodel.getShot_y() + y) / 20;
				if (xd != xdd || yd != ydd) {
					xd = xdd;
					yd = ydd;
					Pathfinding(xd, yd);
				}
			}
		}else if (FightingMixDeal.State == HandleState.USUAL&&e.getButton() == MouseEvent.BUTTON1) {
			/** 鼠标左键移动 屏蔽可以取消*/
//			if (yidong && !Util.dj) {
//				Util.ZOU = false;
//				int x = e.getX();
//				int y = e.getY();
//				int xdd = (Util.mapmodel.getShot_x() + x) / 20;
//				int ydd = (Util.mapmodel.getShot_y() + y) / 20;
//				if (xd != xdd || yd != ydd) {
//					xd = xdd;
//					yd = ydd;
//					Pathfinding(xd, yd);
//				}
//			}
			/** 鼠标左键移动 屏蔽可以取消*/
		}

	}
	@Override
	public void mousePressed(MouseEvent e) {
		if (Util.isM()) {
			return;
		}
		else {
			int x = e.getX();
			int y = e.getY();
			if (e.getButton() == 1) {
				if (x > ScrenceUntil.Screen_x) {
					x -= ScrenceUntil.Screen_x;
					x -= 3;//偏移
					if (y <= ScrenceUntil.ChatFram_y / 10 * 5 - 20) {
						this.inputBean = FrameMessageChangeJpanel.chatbox1.isMonitor(x, y);
						if (this.inputBean != null) {
							this.inputBean.setM(true);
						}
					}
					else {
						if (y >= ScrenceUntil.ChatFram_y / 10 * 5) {
							y -= ScrenceUntil.ChatFram_y / 10 * 5;
							this.inputBean = FrameMessageChangeJpanel.chatbox2.isMonitor(x, y);
							if (this.inputBean != null) {
								this.inputBean.setM(true);
							}
						}
					}
					return;
				}
				else {
					if (FrameMessageChangeJpanel.chatbox2.isDisplay()) {
						final int w = FrameMessageChangeJpanel.chatbox2.getW();
						final int h = FrameMessageChangeJpanel.chatbox2.getH();
						if (x <= w && y >= ScrenceUntil.Screen_y - 45 - h && y < ScrenceUntil.Screen_y - 45) {
							this.inputBean = FrameMessageChangeJpanel.chatbox2.isMonitor(x, y - (ScrenceUntil.Screen_y - 45 - h));
							if (this.inputBean != null) {
								this.inputBean.setM(true);
							}
						}
					}
				}
			}
			if (FightingMixDeal.State == 0) {
				if (GoodsMouslisten.replacepath == -1) {// 右键点击
					if (e.getButton() == 3) {
						// 点击个人信息
						if ("解锁".equals(RoleMsgJframe.getRoleMsgJframe().getJpanel().getLbasuod().getText())) {
							return;
						}
						else {
							FormsManagement.HideForm(77);
							MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
							this.yidong = true;
						}
						// 点击个人信息
					}
					else {
						if (e.getButton() == 1) {
							// 点击个人信息
							if ("解锁".equals(RoleMsgJframe.getRoleMsgJframe().getJpanel().getLbasuod().getText())) {
								FormsManagement.showForm(77);
							}
							else {
								FormsManagement.HideForm(77);
							}
							// 点击个人信息
							if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE12) || MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE13)) {
								MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
							}
							ImageMixDeal.MonitorTrigger(Util.mapmodel.getShot_x() + x, Util.mapmodel.getShot_y() + y);
						}
					}
				}
				else {
					if (e.getButton() == 3) {
						GoodsMouslisten.goodreplace(-1, -1);
					}
					else {
						if (e.getButton() == 1) {
							OptionsJframe.getOptionsJframe().getOptionsJpanel().showBox(TiShiUtil.GoodDiscatd, null, "#W确定要将#G" + GoodsListFromServerUntil.Getgood(GoodsMouslisten.replacepath).getGoodsname() + "#W物品丢弃吗?");
						}
					}
				}
			}
			else {
				if (e.getButton() == 3) {// 右键点击
					if (FightingMonitor.mousestate != 0) {
						if (FightingMixDeal.State == 1) {
							ZhuFrame.getZhuJpanel().ShowManBtn(FightingMixDeal.isLL());
						}
						else {
							if (FightingMixDeal.State == 2) {
								ZhuFrame.getZhuJpanel().ShowBeastBtn();
							}
						}
					}
					FightingMonitor.mousesname = "普通攻击";
					FightingMonitor.mousestate = 0;
					MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
				}
				else {
					if (e.getButton() == 1) {
						FightingMixDeal.MonitorTrigger(e.getX(), e.getY());
					}
				}
			}
			//空白地方移除鼠标法术图标
			GameJpanel.skill = null;
			return;
		}
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseClicked(MouseEvent e) {

	}
	/**寻路*/
	public static void Pathfinding(int x, int y) {

			if (JirenjiaobenchuliControl.name==null) {
                TimeControlRunnable.removeScript();
            }
			XL(x, y);

	}
	/**寻路*/
	public static void XL(int x, int y){
		if (!SendMessageUntil.CanDoConnectOrNo) {
			FrameMessageChangeJpanel.addtext(6, "重连中,时间若太长重上", null, null);
			return;
		}
		if (!BangFight.getBangFight().isChao()) {
			ZhuFrame.getZhuJpanel().addPrompt2("你当前状态被限制");
			return;
		}
		if (x > 2 && x < Util.mapmodel.getW() - 2 && y > 2 && y < Util.mapmodel.getH() - 2) {
			ManimgAttribute.addEffects("点击", x * 20, y * 20);
		}
		RoleShow roleShow = ImageMixDeal.userimg.getRoleShow();
		if (roleShow.getFighting() != 0) {
			return;
		}
		// 处于摆摊状态时不可以移动
		if (roleShow.getBooth_id() != null) {
			return;
		}
		if (JirenjiaobenchuliControl.name==null&&ImageMixDeal.userimg.getTeams() == null) {
			return;
		}

		//飞行器
		boolean i=true;
		if (roleShow.getFly_id()!=0) {
			i=false;
		}
		//跨越地形 七座可飞
		if (roleShow.getFly_id() == 0 && roleShow.getMount_id() < 7 || Util.ditubianma == 3320 || Util.ditubianma == 1732 || Util.ditubianma == 1761
				|| (ImageMixDeal.scene != null && ImageMixDeal.scene.getSceneId() == Scene.DNTGID)) {if (i){
					int rx = roleShow.getX() / 20;
					int ry = roleShow.getY() / 20;
					if (rx < 0 || ry < 0
							|| rx >= Util.mapmodel.getjMap().getMaprules()[0].length
							|| ry >= Util.mapmodel.getjMap().getMaprules().length
							|| Util.mapmodel.mskpanduan(rx, ry)) {
						List<PathPoint> points = new ArrayList<>();
						points.add(new PathPoint(roleShow.getX(), roleShow.getY()));
						points.add(new PathPoint(x * 20, y * 20));
						Path.sendXandYToServer(points);
					} else {
						Path.findPath(Util.mapmodel.getNodes(), rx, ry, x, y);
					}
				}else {
					List<PathPoint> points = new ArrayList<>();
					points.add(new PathPoint(roleShow.getX(), roleShow.getY()));
					points.add(new PathPoint(x * 20, y * 20));
					Path.sendXandYToServer(points);
				}} else {
			List<PathPoint> points = new ArrayList<>();
			points.add(new PathPoint(roleShow.getX(), roleShow.getY()));
			points.add(new PathPoint(x * 20, y * 20));
			Path.sendXandYToServer(points);
		}
	}
	/**点击出来*/
	public static void DJInputBean(InputBean inputBean){
		inputBean.setM(false);
		if (inputBean.getType()==2||inputBean.getType()==3||inputBean.getType()==4) {
			String sendmes = Agreement.getAgreement().richMAgreement(GsonUtil.getGsonUtil().getgson().toJson(inputBean));
	        SendMessageUntil.toServer(sendmes);
		}else if (inputBean.getType()==100){
			final String sendMes = Agreement.getAgreement().stallgetAgreement(inputBean.getId() + "");
			SendMessageUntil.toServer(sendMes);
			// }
        }else if (inputBean.getType()==1||inputBean.getType()==5) {
            String name = "";
            if(inputBean.getName().endsWith("]")){
                name=inputBean.getName().substring(1,inputBean.getName().length()-1);
            }else{
                name=inputBean.getName().substring(1,inputBean.getName().length());
            }
			ZhuFrame.getZhuJpanel().creatroletext(inputBean.getId(), name);
		}else if (inputBean.getType()==11||inputBean.getType()==12) {//申请入队
			ManimgAttribute attribute=ImageMixDeal.huoquLogin(inputBean.getId());
			if (attribute!=null&&attribute.getLeixing()==0) {ZhuFrame.getZhuJpanel().addPrompt2("不能对自己进行操作");return;}
			if (inputBean.getType()==11) {//申请入队
				PlayerMonitor.teamApply(inputBean.getId());
			}else if (inputBean.getType()==12) {//拉人入队
//				RoleShow role=ImageMixDeal.userimg.getRoleShow();
//				if (!(role.getCaptian() != null&&role.getCaptian() == 1)) {ZhuFrame.getZhuJpanel().addPrompt2("你不是队长");return;}
//				RoleShow roleShow = attribute.getRoleShow();
//				if (roleShow.getBooth_id() != null || roleShow.getFighting() != 0) {
//					ZhuFrame.getZhuJpanel().addPrompt("申请人处于繁忙状态！！！");
//					return;
//				}
//				String[] v = AccessTeamInfoUntil.NameS(role.getTeamInfo(),role.getRolename());
//				if (v == null || v.length >= 5) {
//					ZhuFrame.getZhuJpanel().addPrompt2("无法加人");
//					return;
//				}
//				String sendmes = Agreement.getAgreement().TeamAgreeAgreement(roleShow.getRolename());
//				SendMessageUntil.toServer(sendmes);
			}
		}else if (inputBean.getType()==10||inputBean.getType()==20||inputBean.getType()==21||
				inputBean.getType()==22||inputBean.getType()==23||inputBean.getType()==24||inputBean.getType()==25) {
			if (FightingMixDeal.State != HandleState.USUAL) {
				return;
			}
			Coordinates zb=inputBean.getZb();
			if (zb!=null) {
				RoleShow roleShow =ImageMixDeal.userimg.getRoleShow();
				List<Object> list=XLPath.ZDXL(roleShow.getX(), roleShow.getY(), roleShow.getMapid().intValue(), zb.getX(), zb.getY(), zb.getMapID());
			    if (list==null) {
			    	ZhuFrame.getZhuJpanel().addPrompt2("你所在位置无法达到目的地");
					return;
				}
//			       目的类型0系统野怪 1自定义野怪 2击杀NPC 3问候NPC 4给与物品 5护送任务
			    if (inputBean.getType()==21||inputBean.getType()==22||inputBean.getType()==23||inputBean.getType()==25) {
					int type=inputBean.getType()-20;
//					0寻路  1点击NPC 2点击系统野怪  3选择传送选项
					ScriptOpen open=new ScriptOpen(type==1?2:1);
					open.setNpc(inputBean.getId().intValue());
					list.add(0, open);
				}
			    TimeControlRunnable.addScript(list);
			}
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		int x=e.getX(),y=e.getY();
		if (x>ScrenceUntil.Screen_x) {
			x-=ScrenceUntil.Screen_x;
			x-=3;//偏移
		}
		if(ImageMixDeal.userimg.getRoleShow().getFighting() != 0) {
			FightingMixDeal.nameColor(e.getX(), e.getY());
		} else {
			ImageMixDeal.colorName(Util.mapmodel.getShot_x()+x,Util.mapmodel.getShot_y()+y);
		}
	}
}
